/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package healthcare.coursework.service;

import healthcare.coursework.model.MedicalHistory;
import healthcare.coursework.model.Patient;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless(name="MedicalHistoryService")
//@WebService
public class MedicalHistoryServiceImpl implements MedicalHistoryService {

       @PersistenceContext
    private EntityManager em;

    public MedicalHistoryServiceImpl() {
    }

       

    //@WebMethod
    public List<MedicalHistory> listMedicalHistories(Long patientId) {
       Query query=em.createQuery("SELECT m FROM MedicalHistory m WHERE m.isDeleted = 'FALSE' AND m.patient.patientId=?1");
       query.setParameter(1, patientId);
       return query.getResultList();
    }

    //@WebMethod
     public List<MedicalHistory> listMedicalHistoriesByTrackCode(String trackCode) {
       Query query=em.createQuery("SELECT m FROM MedicalHistory m WHERE m.isDeleted = 'FALSE' AND m.patient.trackCode=?1");
       query.setParameter(1, trackCode);
       return query.getResultList();
    }

    //@WebMethod
    public void createMedicalHistory(Long patientId, String result, String medicineRecipe, int weight, int height, int vesel) {
        Patient p=em.find(Patient.class,patientId);
       MedicalHistory mh=new MedicalHistory(medicineRecipe, result, vesel, weight, height);
       mh.setPatient(p);
       mh.setDiagnoseTime(new Date());
       em.persist(mh);
    }

    //@WebMethod
    public void editMedicalHistory(MedicalHistory mh) {
        em.merge(mh);
    }

    //@WebMethod
    public void removeMedicalHistory(Long medicalHistoryId) {
          MedicalHistory mh = em.find(MedicalHistory.class, medicalHistoryId);
        if (mh == null) {
            throw new EntityNotFoundException("The Medical history with id " + medicalHistoryId + " does not exist");
        } else {
            mh.setIsDeleted(true);
            em.merge(mh);
        }
    }

    //@WebMethod
    public void removeMedicalHistories(List<Long> medicalHistoryIds) {
           for (Long medicalHistoryId : medicalHistoryIds) {
            removeMedicalHistory(medicalHistoryId);
        }
    }

    //@WebMethod
    public void recoverMedicalHistory(Long medicalHistoryId) {
          MedicalHistory mh = em.find(MedicalHistory.class, medicalHistoryId);
        if (mh == null) {
            throw new EntityNotFoundException("The Medical history with id " + medicalHistoryId + " does not exist");
        } else {
            mh.setIsDeleted(false);
            em.merge(mh);
        }
    }

    //@WebMethod
    public void removeMedicalHistoryForever(Long medicalHistoryId) {
        em.remove(em.find(MedicalHistory.class, medicalHistoryId));
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 
}
